(function (_super) {
    // var ROLESKIN = {
    //     "1001" : "other/role/girl", //妞妞
    //     "1002" : "other/role/egg",  //皮蛋
    //     "1003" : "other/role/cat",  //乖小猫
    //     "1004" : "other/role/tiger",//帅小虎
    // };
    var MOUNT = {
        "10015" : "tortoise",
        "10016" : "owl",
        "10017" : "ufo",
    }
    var SKILL = {
        "2001"  : "vertigo",
        "2003"  : "silence",
        "2004"  : "escape",
        "10004" : "shield",
        // 2001：晕眩
        // 2002：减速
        // 2003：封印
        // 2004：逃脱
        // 2005：隐身
    }

    function Role(data) {
        Role.super(this);

        this.shadow = null;
        this.roleWrap = null;
        this.roleAct = null;
        this.roleNormal = null;
        this.roleRide = null;
        this.roleInfo = null;
        this.mount = null;
        this.cover = null;
        this.roleStateWrap = null;
        this.bornArrow = null;

        this.roleStateList = {};
        this.isRiding = false;
        this.mountName = null;
        this.direction = null;
        this.moveState = null;

        this.init(data);
    }

    Laya.class(Role, "Game.Role", _super);
    _proto = Role.prototype;
    _proto.destroy = function () {
        _super.prototype.destroy.call(this, true);

        this.shadow = null;
        this.roleWrap = null;
        this.roleAct = null;
        this.roleNormal = null;
        this.roleRide = null;
        this.roleInfo = null;
        this.mount = null;
        this.cover = null;
        this.roleStateWrap = null;
        this.bornArrow = null;

        this.roleStateList = null;
        this.isRiding = false;
        this.mountName = null;
        this.direction = null;
        this.moveState = null;
    }

    _proto.create = function (data) {
        var skin = data.roleId;
        
        var roleWrap = new Laya.Sprite();

        var shadow = new Laya.Image("res/game/role/shadow.png");
            shadow.zOrder = 1;
            shadow.visible = false;
            shadow.anchorX = 0.5;
            shadow.anchorY = 0.5;
            shadow.alpha = 0.5;

        var roleAct = utils.createSkeleton(ROLE_FIGHT_SKIN_PATH + skin + "/act");
            roleAct.zOrder = 3;
            roleAct.visible = false;
        var roleNormal = utils.createSkeleton(ROLE_FIGHT_SKIN_PATH + skin + "/normal");
            roleNormal.zOrder = 3;
            roleNormal.visible = false;
        var roleRide = utils.createSkeleton(ROLE_FIGHT_SKIN_PATH + skin + "/ride");
            roleRide.zOrder = 3;
            roleRide.visible = false;

        var cover = utils.createSkeleton("res/game/role/cover");
            cover.zOrder = 5;
            cover.visible = false;
            cover.y = -50;

        var roleInfo = new Laya.Sprite();
            roleInfo.zOrder = 6;
            roleInfo.visible = false;
            roleInfo.pos(0, -120);

        var roleCamp = new Laya.Image("res/game/role/camp_" + data.camp + ".png");
            roleCamp.anchorX = 0.5;
            roleCamp.anchorY = 0.5;
        roleInfo.addChild(roleCamp);

        if(data.userId == userId){
            var roleMineFlag = new Laya.Image("res/game/role/camp_me.png");
                roleMineFlag.anchorX = 0.5;
                roleMineFlag.anchorY = 0.5;

            roleInfo.addChild(roleMineFlag);
        }
        var roleName = new Laya.Label(data.userName);
            roleName.fontSize = 20;
            roleName.color = "#ffffff";
            roleName.align = "center";
            roleName.bold = true;
            roleName.size(200, 20);
            roleName.pos(-100, -40);

        // roleInfo.cacheAs = "bitmap";
        // roleInfo.cacheAsBitmap = true;
        roleInfo.addChild(roleName);

        var roleStateWrap = new Laya.Sprite();
            roleStateWrap.zOrder = 7;

        var bornArrow = new Laya.Clip("res/game/role/born_arrow.png", 5, 1);
            bornArrow.interval = 200;
            bornArrow.zOrder = 8;
            bornArrow.visible = false;
            bornArrow.anchorX = 0.5;
            bornArrow.anchorY = 1;
            bornArrow.y = -10;

        roleWrap.addChildren(roleAct, roleNormal, roleRide, roleStateWrap, cover);

        this.shadow = shadow;
        this.roleWrap = roleWrap;
        this.roleAct = roleAct;
        this.roleNormal = roleNormal;
        this.roleRide = roleRide;
        this.cover = cover;
        this.roleInfo = roleInfo;
        this.roleStateWrap = roleStateWrap;
        this.bornArrow = bornArrow;
        this.addChildren(shadow, roleWrap, roleInfo, bornArrow);
        
    }
    _proto.init = function (data) {
        this.userId = data.userId;
        this.userName = data.userName;
        this.camp = data.camp;

        this.create(data);
    }

    /**
     * @public
     * 角色向指定方向移动
     * @param {Object} data 角色移动数据
     * 
     * 示例:{"userId":"2037596864","point":{"x":12,"y":18},"direction":"down","stop":0}
     * point:角色当前位置
     * direction:角色当前方向
     * stop:角色当前状态 0:行走 1:停止
     */
    _proto.move = function (data) {
        var moveState = data.stop == 0 ? "move" : "wait";
        var direction = data.direction;
        var role = this.isRiding ? this.roleRide : this.roleNormal;

        this.x = data.point.x * GAME_MOVE_RATE | 0;
        this.y = data.point.y * GAME_MOVE_RATE | 0;

        if (!(this.isRiding && this.mountName == "ufo")) {
            this.zOrder = this.y;
        }

        if(this.direction == direction && this.moveState == "move" && moveState == "move"){return;}
        
        this.moveState = moveState;
        this.direction = direction;

        role.play(moveState + "_" + direction, true);
        this.mount && this.mount.play(moveState + "_" + direction, true);
    }
    _proto.jump = function (point, direction) {
        var endPointX = point.x * GAME_MOVE_RATE;
        var endPointY = point.y * GAME_MOVE_RATE;
        var pos = {};
        var jumpAni = {scaleX : 1.1, scaleY : 1.1};

        this.zOrder = 999;

        switch(direction){
            case "left":
            case "right":
                pos = {x : endPointX};
                jumpAni.y = -50;
                break;
            case "up":
            case "down":
                pos = {y : endPointY};
                break;
        }

        Laya.Tween.to(this, pos, 200, null, Laya.Handler.create(this, function () {
            this.zOrder = this.y;
        }));
        Laya.Tween.to(this.roleWrap, jumpAni, 100, null, Laya.Handler.create(this.roleWrap, function () {
            Laya.Tween.to(this, {y : 0, scaleX : 1, scaleY : 1}, 100);
        }));

    }
    /**
     * 骑乘坐骑
     * @param {String} type mount:骑乘 dismount:下坐骑
     * @param {String} mountId 坐骑ID
     */
    _proto.ride = function (type, mountId) {
        Laya.timer.clearAll(this);
        this.roleAct.visible = false;
        this.roleAct.stop();

        switch(type){
            case "mount":
                this.mount && this.mount.destroy(true);
                var name = MOUNT[mountId];
                var mount = utils.createSkeleton("res/game/mount/" + name);
                    mount.zOrder = name == "ufo" ? 4 : 2;
                    mount.play(this.moveState + "_" + this.direction, true);

                if(name == "ufo"){
                    this.zOrder = 999;
                }
                this.roleInfo.y = -180;
                this.roleStateWrap.y = -60;
                this.roleRide.visible = true;
                this.roleRide.play(this.moveState + "_" + this.direction, true);
                this.roleNormal.visible = false;
                this.roleNormal.stop();

                this.isRiding = true;
                this.mountName = name;
                this.mount = mount;
                this.roleWrap.addChild(mount);
                break;
            case "dismount":
                this.zOrder = this.y;
                this.roleInfo.y = -120;
                this.roleStateWrap.y = 0;
                this.roleRide.visible = false;
                this.roleRide.stop();
                this.roleNormal.visible = true;
                this.roleNormal.play(this.moveState + "_" + this.direction, true);

                this.isRiding = false;
                this.mountName = null;

                this.mount && this.mount.destroy(true);
                this.mount = null;
                break;
        }
    }
    _proto.changeState = function (type, skillId, alpha) {
        switch(type){
            case "hit":
                switch(skillId){
                    case "2002":// 2002：减速
                        //todo
                        break;
                    case "2001":// 2001：晕眩
                    case "2003":// 2003：封印
                        if(!this.roleStateList[skillId]){
                            var state = utils.createSkeleton("res/game/state/" + SKILL[skillId]);
                                state.play(0, true);

                            this.roleStateList[skillId] = state;
                            this.roleStateWrap.addChild(state);
                        }
                        break;
                    case "2004":// 2004：逃脱
                        var state = utils.createSkeleton("res/game/state/" + SKILL[skillId]);
                            state.play(0, false);
                            state.once(Laya.Event.STOPPED, state, function () {
                                Laya.timer.callLater(this, function () {
                                    this.destroy(true);
                                });
                            });

                        this.roleStateWrap.addChild(state);
                        break;
                    case "2005":// 2005：隐身
                        this.alpha = alpha;
                        break;
                    case "10004":// 10004：盾牌
                        if(!this.roleStateList[skillId]){
                            var state = utils.createSkeleton("res/game/state/" + SKILL[skillId]);
                                state.play("enter", false);
                                state.once(Laya.Event.STOPPED, state, function () {
                                    this.play("loop", true);
                                });

                            this.roleStateList[skillId] = state;
                            this.roleStateWrap.addChild(state);
                        }
                        break;
                }
                break;
            case "release":
                switch(skillId){
                    case "2002":// 2002：减速
                        //todo
                        break;
                    case "2001":// 2001：晕眩
                    case "2003":// 2003：封印
                        if(this.roleStateList[skillId]){
                            this.roleStateList[skillId].destroy(true);
                            delete this.roleStateList[skillId];
                        }
                        break;
                    case "10004":// 10004：盾牌
                        if(this.roleStateList[skillId]){
                            console.log(skillId, this.roleStateList);
                            this.roleStateList[skillId].once(Laya.Event.STOPPED, this, function () {
                                console.log(skillId, this.roleStateList);
                                Laya.timer.callLater(this, function () {
                                    console.log(skillId, this.roleStateList);
                                    if(this.roleStateList[skillId]){
                                        this.roleStateList[skillId].destroy(true);
                                        delete this.roleStateList[skillId];
                                    }
                                });
                            });
                            this.roleStateList[skillId].play("exit", false);
                        }
                        break;
                    case "2004":// 2004：逃脱
                        //todo
                        break;
                    case "2005":// 2005：隐身
                        this.alpha = 1;
                        break;
                }
                break;
        }
    }
    /**
     * 角色动作
     * @param {String} act 动作名
     * 动作名包含 高兴:happy 困住:trap 死亡:die(播放一次) 被解救:release(播放一次)
     */
    _proto.act = function (act) {
        console.log("Role Act:" + act);
        this.isRiding && this.ride("dismount");
        
        for(var i in this.roleStateList){
            this.roleStateList[i].destroy(true);
            delete this.roleStateList[i];
        }

        this.roleAct.visible = false;
        this.roleNormal.visible = false;
        this.roleRide.visible = false;
        this.cover.visible = false;

        switch(act){
            case "happy":
                this.roleAct.visible = true;

                this.roleAct.play("happy", true);
                break;
            case "trap":
                this.cover.visible = true;
                this.roleAct.visible = true;

                this.cover.play("trap", true);
                this.roleAct.play("trap", true);
                break;
            case "die":
                this.cover.visible = true;
                this.roleAct.visible = true;
                
                this.cover.play("boom", false);
                this.cover.offAll(Laya.Event.STOPPED);

                this.roleAct.play("die", false);
                this.roleAct.once(Laya.Event.STOPPED, this, function () {
                    console.log("角色死亡");
                    this.visible = false;
                });
                break;
            case "release":
                this.roleNormal.visible = true;

                this.cover.play("boom", false);
                this.cover.offAll(Laya.Event.STOPPED);
                this.cover.once(Laya.Event.STOPPED, this, function () {
                    this.cover.visible = false;
                    this.roleNormal.play("wait_down", true);
                });
                break;
        }
    }
    _proto.cross = function (type, isSameCamp) {
        switch(type){
            case "enter":
                isSameCamp ? this.roleWrap.visible = false : this.visible = false;
                break;
            case "exit":
                isSameCamp ? this.roleWrap.visible = true : this.visible = true;
                break;
        }
    }
    /**
     * 开始游戏
     */
    _proto.start = function () {
        this.bornArrow.destroy(true);
        this.bornArrow = null;

        this.roleInfo.visible = true;
        this.roleNormal.visible = true;

        this.roleNormal.play("wait_down", true);
    }
    /**
     * 角色出生动画
     */
    _proto.born = function (point) {
        this.pos(point.x * GAME_MOVE_RATE, point.y * GAME_MOVE_RATE);

        this.zOrder = this.y;
        
        this.shadow.visible = true;
        this.bornArrow.visible = true;
        this.bornArrow.play();
    }
    /**
     * 角色重生
     */
    _proto.reBorn = function (point) {
        this.cover.visible = false;
        this.roleAct.visible = false;

        this.roleNormal.visible = true;
        this.roleNormal.play("wait_down", true);

        this.pos(point.x * GAME_MOVE_RATE, point.y * GAME_MOVE_RATE);
        this.zOrder = this.y;
        this.visible = true;
    }
})(Laya.Box);